#pragma once
#include<iostream>
#include<cstdlib>
#include<ctime>
#define TASK_SIZE 3

typedef void(*task_t)();
task_t tasks[TASK_SIZE];
void PrintTask()
{
    std::cout<<"正在执行打印任务中......"<<std::endl;
}
void DownLoad()
{
    std::cout<<"正在执行下载任务中......"<<std::endl;
}
void Flush()
{
    std::cout<<"正在执行刷新缓冲区任务中......"<<std::endl;
}

void LoadTasks()
{
    //种下时间种子
    srand(time(nullptr)^314231);
    tasks[0] = PrintTask;
    tasks[1] = DownLoad;
    tasks[2] = Flush;
}

void EecuteTask(int index)
{
    if(index < 0 || index > TASK_SIZE-1)
    {
        std::cout<<"Task执行失败!,请重试"<<std::endl;
        return;
    }
    tasks[index]();
}
//随机选择任务
int SelectTask()
{
    return rand() % TASK_SIZE;
}

void SubProcessWork()
{
    while (true)
    {
        int TaskCommand = 0;
        int n = read(0, &TaskCommand, sizeof TaskCommand);
        if (n == sizeof(int))
        {
            EecuteTask(TaskCommand);
        }
        else
        {
            std::cout << "TASK执行完毕,子进程退出" << std::endl;
            break;
        }
    }
}